package People;

import java.sql.*;

public class Anagrafica {
	private int id;
	private String name;
	private String surname;
	private String bornDate;
	private String bornCity;
	private Address residence;
    private Address domicile;
	private int state;
	private int maritalState;
    
    Anagrafica() {
    		this.id = 0;
    }
    
	Anagrafica(int id) {
		this.id = id;

		if (id != 0) {
			try {
				Connection c = Utilities.connection;
				Statement s = c.createStatement();
		
				ResultSet r = s.executeQuery("SELECT * FROM person WHERE id = " + this.id);
				
				if (r.next()) {
				 	this.name = r.getString(2);
				 	this.surname = r.getString(3);
					this.bornDate = r.getString(4);
				 	this.bornCity = r.getString(5);
					this.residence = new Address(r.getString(6), r.getString(7), r.getString(8), "");
					this.domicile = new Address(r.getString(9), r.getString(10), r.getString(11), "");
					this.maritalState = r.getInt(12);
				 	this.state = r.getInt(13);
				} else {
					// there is no people with this id
				}
			    r.close();				
				s.close();
					    
			} catch (SQLException e) {
				System.out.println(e.toString());
			}
		}
	}
	
	public void save() {
		if (this.id == 0) {
			try {
				Connection c = Utilities.connection;
				Statement s = c.createStatement();
				
				s.executeUpdate("INSERT INTO person (name) VALUES ('NEW NAME')", Statement.RETURN_GENERATED_KEYS);

				ResultSet r = s.getGeneratedKeys();
				if (r.next())
				 	this.id = r.getInt(1);
			    r.close();

			} catch (SQLException e) {
				System.out.println(e.toString());
			}
		}
		
		update();
	}
/*				s.executeUpdate("UPDATE person " + 
								"SET name = '" + this.name + "'," + 
								"surname = '" + this.surname + "'," + 
								//"bornDate = '" + this.bornDate + "'," + 
								"bornCity = '" + this.bornCity + "'," + 
								"residence_street = '" + this.residence.getStreet() + "'," + 
								"residence_houseNumber = '" + this.residence.getHouseNumber() + "'," + 
								"residence_city = '" + this.residence.getCity() + "'," + 
								"domicile_street = '" + this.domicile.getStreet() + "'," + 
								"domicile_houseNumber = '" + this.domicile.getHouseNumber() + "'," + 
								"domicile_city = '" + this.domicile.getCity() + "'," + 
								"maritalState = '" + this.maritalState + "'," + 
								"state = '" + this.state + "' " +
								"WHERE id = " + id);*/
	private void update() {
		try {	    
			Connection c = Utilities.connection;
			Statement s = c.createStatement();

			if (this.name != null)
				s.executeUpdate("UPDATE person SET name = '" + this.name + "' WHERE id = " + id);
			if (this.surname != null)
				s.executeUpdate("UPDATE person SET surname = '" + this.surname + "' WHERE id = " + id);
			if (this.bornDate != null)
				s.executeUpdate("UPDATE person SET bornDate = '" + this.bornDate + "' WHERE id = " + id);
			if (this.bornCity != null)
				s.executeUpdate("UPDATE person SET bornCity = '" + this.bornCity + "' WHERE id = " + id);
			if (this.residence.getStreet() != null)
				s.executeUpdate("UPDATE person SET residence_street = '" + this.residence.getStreet() + "' WHERE id = " + id);
			if (this.residence.getHouseNumber() != null)
				s.executeUpdate("UPDATE person SET residence_houseNumber = '" + this.residence.getHouseNumber() + "' WHERE id = " + id);
			if (this.residence.getCity() != null)
				s.executeUpdate("UPDATE person SET residence_city = '" + this.residence.getCity() + "' WHERE id = " + id);
			if (this.domicile.getStreet() != null)
				s.executeUpdate("UPDATE person SET domicile_street = '" + this.domicile.getStreet() + "' WHERE id = " + id);
			if (this.domicile.getHouseNumber() != null)
				s.executeUpdate("UPDATE person SET domicile_houseNumber = '" + this.domicile.getHouseNumber() + "' WHERE id = " + id);
			if (this.domicile.getCity() != null)
				s.executeUpdate("UPDATE person SET domicile_city = '" + this.domicile.getCity() + "' WHERE id = " + id);
			s.executeUpdate("UPDATE person SET maritalState = " + this.maritalState + " WHERE id = " + id);
			s.executeUpdate("UPDATE person SET state = " + this.state + " WHERE id = " + id);

			s.close();
		
		} catch (SQLException e) {
			System.out.println(e.toString());
		}
	}
	
	public String getId() {
		return "" + this.id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSurname() {
		return this.surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public int getState() {
		return this.state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public Address getResidence() {
		return residence;
	}

	public void setResidence(Address residence) {
		this.residence = residence;
	}
	
	public Address getDomicile() {
		return domicile;
	}

	public void setDomicile(Address domicile) {
		this.domicile = domicile;
	}

	public String getBornDate() {
		return bornDate;
	}

	public void setBornDate(String bornDate) {
		this.bornDate = bornDate;
	}

	public String getBornCity() {
		return bornCity;
	}

	public void setBornCity(String bornCity) {
		this.bornCity = bornCity;
	}
	
	public int getMaritalState() {
		return maritalState;
	}

	public void setMaritalState(int maritalState) {
		this.maritalState = maritalState;
	}
}
